<!doctype html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>全部人员管理</title>
    <script src="/webjars/miniui/boot.js" type="text/javascript"></script>
    <link href="/static/css/resource.css" rel="stylesheet" type="text/css"/>
    <script src="/static/js/common/common.js" type="text/javascript"></script>
    <link href="/webjars/miniui/css/demo.css" rel="stylesheet" type="text/css"/>
    <link href="/static/css/default.css" rel="stylesheet" type="text/css"/>
    <style>
        .discell {
            text-align: right;
            width: 15%;
        }

        .input {
            width: 20%;
        }
        .legend{
            line-height: 16px;
            font-size: 14px;
            font-weight: bold;
            border-left: 3px solid #4A89FD;
            padding-left: 8px;
            margin: 18px 0 10px;
        }
    </style>
</head>
<body>

<div class="mini-fit" style="box-sizing: border-box;margin-bottom:45px;">
    <form id="form1" method="post">

        <input id="id" name="id" class="mini-hidden" value="${id!''}" style="width: 100%;" readonly/>
        <input id="nowuserid" name="nowuserid" class="mini-hidden" value="${nowuserid!''}" style="width: 100%;"
               readonly/>
        <input id="priNum" name="priNum" class="mini-hidden" value="${priNum!''}" style="width: 100%;"
               readonly/>
        <input id="pridepartnum" name="pridepartnum" class="mini-hidden" value="${pridepartnum!''}"
               style="width: 100%;" readonly/>

        <!-- 用户 编辑框START-->
        <div id="editform" class="form" style="padding:10px 20px;">
            <table class="form-table" align="center" width="100%">
                <tr>
                    <td width="30%">
                        <span style="font-weight: bold;font-size: 20px;margin: 10px 0 0;">新增安置帮教小组</span>
                    </td>
                    <td width="70%">

                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <div>
                            <div class="legend">待选人员</div>
                            <div id="editform3" class="form" style="background: #f6f6f6;padding: 3px 10px 3px 0;">
                                <table class="form-table" width="100%" style="white-space:nowrap">
                                    <tr>
                                        <td width="120" align="right"> 安帮工作人员:</td>
                                        <td>
                                            <input id="gzry" class="mini-checkboxlist" repeatItems="10"
                                                   repeatLayout="table"
                                                   textField="text" valueField="id"
                                                   url="/azbj/bjdxInfo/setBjxz/getWorkerList?priNum=${priNum!''}"/>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td width="120" align="right"><span style="color:darkgray">说明:</span></td>
                                        <td>
                                            <span style="color:darkgray">(工作人员包括：司法机构安置帮教工作人员、从事安置帮教工作的社会工作者、其他)</span>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="right">志愿者:</td>
                                        <td>
                                            <div id="zyz" class="mini-checkboxlist" repeatItems="10"
                                                 repeatLayout="table"
                                                 textField="text" valueField="id"
                                                 url="/azbj/bjdxInfo/setBjxz/getZyzList?priNum=${priNum!''}"></div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="right">稳控民警:</td>
                                        <td>
                                            <div id="wkmj" class="mini-checkboxlist" repeatItems="10"
                                                 repeatLayout="table"
                                                 textField="text" valueField="id"
                                                 url="/azbj/bjdxInfo/setBjxz/getWkmjList?priNum=${priNum!''}"></div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="right"> 家庭成员:</td>
                                        <td>
                                            <div id="jtcy" class="mini-checkboxlist" repeatItems="10"
                                                 repeatLayout="table"
                                                 textField="text" valueField="id"
                                                 url="/azbj/bjdxInfo/setBjxz/getFamilyList?priNum=${priNum!''}"></div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td colspan="2" align="right" style="line-height: 30px;"><a
                                                    class="mini-button mini-button-success"
                                                    onclick="insertRow()">确定</a></td>
                                    </tr>
                                </table>
                            </div>
                        </div>
                    </td>
                    <td>

                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <div class="legend">小组成员</div>
                        <a class="mini-button mini-button-primary"   onclick="addRow()">新增</a>
                        <div id="datagrid1" class="mini-datagrid" ajaxType="get" idField="id"
                             autoload="true" allowCellEdit="true" allowCellSelect="true"
                             oncellbeginedit="cellbeginedit"  allowAlternating="true"
                             multiSelect="true" showColumns="true" showPager="false"
                             url="/azbj/bjdxInfo/setBjxz/getBjxzcyList?priNum=${priNum!''}">
                            <div property="columns">
                                <div  name="name" field="name" width="20px" align="center" headerAlign="center" allowSort="false">姓名
                                    <input id="name" property="editor" class="mini-textbox" style="width:100%;"/>
                                </div>
                                <div name="relation" field="relation" width="20px" renderer="onZzRenderer"
                                     align="center" headerAlign="center" allowSort="false">身份
                                    <#if "${localCode!''}" == "140000">
                                        <input property="editor" textField="text" valueField="id" class="mini-combobox"
                                               data="[{ id: '02', text: '组长' },{ id: '05', text: '机构人员' },{ id: '06', text: '稳控民警' },{ id: '07', text: '家庭成员' }
                                               ,{ id: '08', text: '社区工作者' },{ id: '09', text: '社会工作者' },{ id: '10', text: '社会志愿者' }]" />
                                    <#else>
                                        <input property="editor" textField="text" valueField="id" class="mini-combobox"
                                               data="[{ id: '02', text: '组长' },{ id: '03', text: '组员' },{ id: '04', text: '稳控民警' }]" />
                                    </#if>
                                </div>
                                <div name="sex" field="sex" width="12px" renderer="onGenderRenderer" align="center"
                                     headerAlign="center">性别
                                    <input property="editor" textField="text" valueField="id" class="mini-combobox"
                                           data="[{ id: 1, text: '男性' },{ id: 2, text: '女性' },{ id: 0, text: '未知的性别' },{ id: 9, text: '未说明的性别' }]"/>
                                </div>

                                <div name="age" field="age" width="12px" align="center" headerAlign="center"
                                     allowSort="false"> 年龄
                                    <input property="editor" class="mini-spinner" vtype="range:0,100" style="width:100%;"/>
                                </div>
                                <div name="danwei" field="danwei" width="25px" align="center" headerAlign="center"
                                     allowSort="false">工作单位及职务
                                    <input property="editor" class="mini-textbox" style="width:100%;"/>
                                </div>
                                <div name="phone" field="phone" width="20px" align="center" headerAlign="center"
                                     allowSort="false">手机号码
                                    <input id="phone" property="editor" class="mini-textbox" style="width:100%;" />
                                </div>
                                <div name="fixedLine" field="fixedLine" width="20px" align="center" headerAlign="center"
                                     allowSort="false">固话
                                    <input id="fixedLine" property="editor" class="mini-textbox" style="width:100%;"/>
                                </div>
                                <!--隐藏区域，目的：将上面的phone和fixedLine拼接好之后组装成phone | fixedLine的格式，放入tel中，
                                因为后台直接将前台传递的数据转成AbXiaozuResultVo，AbXiaozuResultVo中只有tel字段，没有phone，所以在此做处理
                                -->
                                <div name="tel" field="tel" width="20px" align="center" headerAlign="center" visible="false" allowSort="false">固话
                                    <input id="tel" property="editor" class="mini-textbox" style="width:100%;"/>
                                </div>
                                <div name="action" width="20px" align="center" headerAlign="center"
                                     renderer="onActionRenderer" allowSort="false">操作
                                </div>
                            </div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <div class="legend">帮教工作方案</div>
                        <div id="xbjfa" name="xbjfa" value="${xbjfa!''}" style="width:100%; height: 80px;"
                             emptyText="请输入内容(0-100):" class="mini-textarea" maxlength="100">
                        </div>
                    </td>
                </tr>
            </table>
        </div>
    </form>

    <form id="form2" method="post">
        <!-- 用户 编辑框START-->
        <div id="fileform" class="form" style="padding-left: 20px;padding-bottom: 20px;">
            <table class="form-table" align="center" width="100%">
                <tr>
                    <td colspan="2">
                        <div class="legend">附件上传</div>
                    </td>
                </tr>
                <tr>
                    <td style="padding-left: 80px;">
                        <input style="background: #4A89FD;color: #fff;outline: none;border: none;height: 25px;" type="button" value="上传附件" onclick="onFileUpload()">
<#--                        <span  style="color: #8B8B8B" > 支持扩展名：.rar .zip .doc .docx .pdf .jpg...</span>-->
                        <span id="msg"></span>
                        <div><b>支持扩展名:</b>.rar .zip .doc .docx .xls .xlsx .pdf .jpg</div>
                    </td>
                    <td style="color: #8B8B8B;font-size:8px;">

                    </td>
                </tr>
            </table>
        </div>

    </form>

</div>

<div style="border-top: 1px solid #dfdfdf;text-align: center;position: absolute;bottom: 0;width: 100%;height:45px;line-height: 45px;text-align: center">
    <a class="mini-button" iconCls="fa-save" plain="true" onclick="saveData()">保存</a>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
    <a class="mini-button" iconCls="fa-undo" plain="true" onclick="close()">取消</a>
</div>

<script type="text/javascript">
    mini.parse();
    var form = new mini.Form("form1");
    var priNumObj = mini.get("priNum");
    var gzryObj = mini.get("gzry");
    var zyzObj = mini.get("zyz");
    var jtcyObj = mini.get("jtcy");
    var fDataObj = mini.get("fData");
    var xbjfaObj = mini.get("xbjfa");
    var idObj = mini.get("id");
    var pridepartnumObj = mini.get("pridepartnum");
    var appendixObj = document.getElementById("appendix");
    var nowuseridObj = mini.get("nowuserid");
    var wkmjObj = mini.get("wkmj");
    var grid = mini.get("datagrid1");
    var sex1 = mini.get("sex");

    // 页面一加载
    window.onload = function (){
        $("#msg").text("上传成功" + ${count1} + "条!")
    }

    function insertRow() {
        //grid.reload();
        var gzryVal = gzryObj.getValue();
        var gzryids = getStrSplit(gzryVal);
        var zyzVal = zyzObj.getValue();
        var zyzids = getStrSplit(zyzVal);
        var wkmjval = wkmjObj.getValue();
        var wkmjids = getStrSplit(wkmjval);
        var jtcyVal = jtcyObj.getValue();
        var jtcyids = getStrSplit(jtcyVal);
        if (gzryids || zyzids || jtcyids) {
            $.ajax({
                url: "/azbj/bjdxInfo/setBjxz/getAddXzcyList",
                type: "post",
                data: {gzryids: gzryids, zyzids: zyzids, jtcyids: jtcyids, wkmjids: wkmjids},
                success: function (text) {
                    //勾选的数据
                    var o = mini.decode(text);
                    //现有数据条数
                    var leng = grid.getData().length;
                    //现有数据
                    var oo = grid.getData();
                    //新数组，去重后的数据
                    var myCars = [];
                    var k = 0;
                    var index = 0;
                    //循环勾选数据
                    for (j = 0, len = o.length; j < len; j++) { //赵六 李四  神奇
                        var flag = false;
                        //循环现有数据
                        for (i = 0; i < leng; i++) { //张三 李四 王五
                            //如果勾选数据的parentId 和 现有数据的parentId 相同
                            if (o[j].parentId == oo[i].parentId) {
                                //重复数据，设置true
                                flag = true;
                            } else {
                                index = j;
                            }
                        }
                        //如果不是重复数据，则添加到新数组
                        if(!flag){
                            myCars[k] = o[index];
                            k++;
                        }
                    }
                    grid.addRows(myCars, leng);
                }
            });
        }
    }

    function getStrSplit(val) {
        var str = "'";
        var strArr = val.split(",");
        for (var i = 0; i < strArr.length; i++) {
            str += strArr[i] + "','";
        }
        str = str.substr(0, str.length - 2);
        return str;
    }

    function onGenderRenderer(e) {
        var val = e.value;
        var res = "";
        if(val == '1') {
            res = "男性";
        } else if (val == '2') {
            res = "女性";
        } else if (val == '0') {
            res = "未知的性别";
        } else if (val == '9') {
            res = "未说明的性别";
        }
        return res;
    }

    function onZzRenderer(e) {
        var val = e.value;
        var res = "";
        if(${localCode!''} == "140000") {
            if (val == '01' || val == '1') {
                res = "帮教对象";
                e.record.cancel = true;
            } else if (val == '02') {
                res = "组长";
            } else if (val == '05') {
                res = "机构人员";
            } else if (val == '06') {
                res = "管控民警";
            } else if (val == '07') {
                res = "家庭成员";
            } else if (val == '08') {
                res = "社区工作者";
            } else if (val == '09') {
                res = "社会工作者";
            } else if (val == '10') {
                res = "社会志愿者";
            }
        } else {
            if (val == '01' || val == '1') {
                res = "帮教对象";
                e.record.cancel = true;
            } else if (val == '02') {
                res = "组长";
            } else if (val == '03') {
                res = "组员";
            } else if (val == '04') {
                res = "稳控民警";
            }
        }
        return res;
    }

    function saveData() {
        var gData = grid.getData();
        var hasZz = 0;
        // 山西个性化需求,组长,机构人员,稳控民警必填
        var hasjgry = 0;
        var hasmj = 0;
        var sf = "";
        for (var i = 0; i < gData.length; i++) {
            if (gData[i].relation == "02") {
                hasZz++;
            }
            if (gData[i].relation == "05") {
                hasjgry++;
            }
            if (gData[i].relation == "06") {
                hasmj++;
            }
            if (!gData[i].relation) {
                sf += gData[i].name + ',';
            }
            var dh = gData[i].fixedLine==undefined?'':gData[i].fixedLine;
            var phone = gData[i].phone==undefined?'':gData[i].phone;
            if (!checkTel(dh) || !checkPhone(phone)) {
                mini.alert("手机号码或固定电话有误,请重填");
                return;
            }else {
                gData[i].tel = phone+"|"+dh;
            }
        }
        if (sf) {
            sf = sf.substr(0, sf.length - 1);
            mini.alert(sf + "的身份不能为空");
            return;
        }
        if (hasZz == 0 && gData.length > 1) {
            mini.alert("至少有一个组长");
            return;
        }
        if (hasZz > 1 && gData.length > 1) {
            mini.alert("只能有一位组长");
            return;
        }
        if (${localCode} == "140000") {
            if(hasjgry == 0) {
                mini.alert("至少有一个机构人员");
                return;
            }
            if(hasmj == 0) {
                mini.alert("至少有一个管控民警");
                return;
            }
        }
        //modified,added,removed
        var rowsAdd = grid.getChanges("added");
        var noName = 0;
        for (var i = 0; i < rowsAdd.length; i++) {
            if (!rowsAdd[i].name) {
                noName++;
            }
        }
        if (noName > 0) {
            mini.alert("新添人员姓名不能为空");
            return;
        }

        var rowsDel = grid.getChanges("removed");
        var rowsEdit = grid.getChanges("modified");
        var delIds = "";

        for (var i = 0; i < rowsDel.length; i++) {

            delIds += rowsDel[i].id + ",";
        }
        var xbjfa = xbjfaObj.getValue();
        var priNum = priNumObj.getValue();
        var data = {};
        data.dataAdd = rowsAdd;
        data.dataEdit = rowsEdit;
        data.delIds = delIds;
        data.xbjfa = xbjfa;
        data.priNum = priNum;
        var json = mini.encode(data);
        $.ajax({
            url: "/azbj/bjdxInfo/qbrygl/saveBjxz",
            type: "post",
            data: {data: json},
            success: function (success) {
                if(success.code == 1) {
                    mini.alert("保存成功！","保存提示",function () {
                        grid.reload();
                        refreshPage("close");
                    });
                } else if (success.code == 5599) {
                    mini.alert(success.message);
                }
            }
        });
    }


    function onActionRenderer(e) {
        if (e.record.priNum) {
            return "";
        }
        var s = "";

        if('1' != e.record.relation){
            s += '&nbsp;<a class="mini-button"  name="midbtn_del"  onclick="delRow()"  >&nbsp;删除&nbsp;</a>&nbsp';

        }
        return s;
    }

    function delRow() {
        var row = grid.getSelected();
        var nowuserid = nowuseridObj.getValue();
        if (row.adduserid && nowuserid != row.adduserid) {
            mini.alert("非本人添加，没有操作权限");
            return;
        }
        grid.removeRow(row, true);
    }
    function cellbeginedit(e) {
        if (e.record.priNum) {
            e.cancel = true;
        }
    }

    function addRow() {
        if(${localCode!''}=="140000") {
            var newRow = {xttype: "0", relation: "05"};
        } else {
            var newRow = {xttype: "0", relation: "03"};
        }
        var length = grid.getData().length;
        grid.addRow(newRow, length);
    }

    function onFileUpload(e) {
        var idval = idObj.getValue();
        var prival = priNumObj.getValue();
        var taskid = "24";
        var url = "/upload/user/uploadAppendixfile/view?1=1&id="+idval+"&priNum="+prival+"&taskId=" + taskid;
        commonEdit(url, null, null,600, 350, true,true,true,callback);
        // 回调函数
        function callback() {
            $.ajax({
                url: "/upload/downloadAppendixFile/getCountall?1=1&priNum="+prival+"&taskId=24",
                type: "get",
                success: function (success) {
                    $("#msg").text("上传成功" + success.data + "条!");
                }
            })
        }
    }

    function refreshPage(action) {
        if (action == "close") {
            window.location.reload();
        }
    }

    function close(action) {
        if (window.CloseOwnerWindow) {
            if(${localCode} == "140000") {
                window.CloseParentWindow(action);
            }
            window.CloseOwnerWindow(action);
        } else {
            window.close();
        }
    }

    function checkTel(tel) {
        if(tel == undefined || tel == ''){
            return true;
        }
        if(!(/^((0\d{2,3})-?)(\d{7,8})$/.test(tel)) || !(/^(0(10|21|22|23|[1-9][0-9]{2})(-|))?[0-9]{7,8}$/.test(tel))){
            return false;
        }
        return true;
    }

    //电话号码的验证
    function checkPhone(phone) {
        if(phone == undefined || phone==''){
            return true;
        }
        if(!(/^1[3456789]\d{9}$/.test(phone))){
            return false;
        }
        return true;
    }
</script>
</body>
</html>